#include "room.h"
#include "../common/streamer.h"
#include <QJsonObject>
#include <QByteArray>
#include <QJsonDocument>

Room::~Room()
{
//    Audience.clear();
//    AudienceNames.clear();
}

Room::Room(QObject *parent) : QObject(parent)
{
    roomID = nullptr;
    roomHost = nullptr;

}

Room::Room(QString& id, QString& host,Socket *anchor)
{
    roomID = id;
    roomHost = host;
    Audience.push_back(anchor);
    AudienceNames.push_back(host);
}

void Room::addAudience(Socket *audience)
{
    //后加入观众
    Audience.push_back(audience);
    AudienceNames.push_back(audience->getUserName());

}

void Room::deleAudience(Socket *audience)
{
    //先删除该观众
    Audience.removeOne(audience);
    AudienceNames.removeOne(audience->getUserName());

    //群发通知删除该观众
    QJsonObject json = QJsonObject();
    json["username"] = audience->getUserName();
    QByteArray data = QJsonDocument(json).toJson();

    for(auto s : Audience){
        s->write(Streamer::pack(Handler::deleAudience,data));
    }
}

void Room::chatBroadcast(QByteArray *data)
{
    for(auto s : Audience){
        s->write(Streamer::pack(Handler::chat,*data));
    }
}

void Room::offBroadcast(QByteArray* data)
{
    for(auto s : Audience){
        s->write(Streamer::pack(Handler::closeRoom,*data));
    }
}

void Room::addAudiBroadcast(QByteArray *data)
{

    for(auto s : Audience){
        s->write(Streamer::pack(Handler::addAudience,*data));
    }
}

void Room::audioBroadcast(const QByteArray &data)
{
    for(auto s : Audience){
        s->write(Streamer::pack(Handler::audioTrans,data));
    }
}

void Room::videoBroadcast(Socket* socket,const QByteArray &data)
{
    for(auto s : Audience){
        //if(s==socket)continue;
        s->write(Streamer::pack(Handler::videoTrans,data));
    }
}

